package com.zhx.threadcoreknowledge.stopthread.volatiledemo;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * @author ：zhanghaixuan
 * @date ：Created in 2019-10-01 10:15
 * 用途         ： 无法使用volatile停止线程的情况, 当线程陷入阻塞的时候 volatile是无法停止的
 *
 * 这个例子中, 生产者的生产速度很快, 消费者消费速度慢
 */
public class WrongWatVolatileCantStop {

    public static void main(String[] args) throws InterruptedException {
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(10);

        Producer producer = new Producer(arrayBlockingQueue);
        Thread thread = new Thread(producer);
        thread.start();
        try {
            Thread.sleep(1500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Comsumer comsumer = new Comsumer(arrayBlockingQueue);

        while (comsumer.needMoreNums()){
            System.out.println(comsumer.storage.take()+"被消费了");
            Thread.sleep(100);
        }
        System.out.println("消费者不需要更多数据了");

        producer.canceled = true;
    }
}


class Producer implements Runnable{

    public volatile boolean canceled = false;

    BlockingQueue strage;

    public Producer(BlockingQueue strage) {
        this.strage = strage;
    }

    @Override
    public void run() {
        int num = 0;
        try {
            while (num <= 1000000 && !canceled) {
                if (num % 100 == 0) {
                    strage.put(num);
                    System.out.println(num + "是100的倍数");
                }
                Thread.sleep(0);
                num ++;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            System.out.println("生产者停止运行");
        }
    }
}

class  Comsumer{
    BlockingQueue storage;

    public Comsumer(BlockingQueue storage) {
        this.storage = storage;
    }
    
    public boolean needMoreNums(){
        if (Math.random()>0.95) {
            return false;
        }
        return true;
    }
    
    
}
